Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverted: use optimised ByteString.asInputStream #539

Merged
merged 5 commits into from
Apr 25, 2024

Conversation

pjfanning
Copy link
Contributor

@pjfanning pjfanning commented Apr 19, 2024

  • ByteString.asInputStream is only in Pekko 1.1 - so if you use this code with Pekko 1.0, the MethodHandle will not work and the code falls back to existing ByteArrayInputStream

@pjfanning
Copy link
Contributor Author

I benchmarked this using https://github.com/pjfanning/pekko-test/blob/main/bench/src/main/scala/org/example/pekko/ByteStringBench.scala

The new code (in this PR) was faster for byte strings that are made up of multiple byte strings but slower for simple byte strings.

[info] Benchmark                                 Mode  Cnt       Score        Error  Units
[info] ByteStringBench.newGetInputStream        thrpt    3     752.065 ±    787.928  ops/s
[info] ByteStringBench.newGetInputStreamSimple  thrpt    3  447966.746 ± 273762.965  ops/s
[info] ByteStringBench.oldGetInputStream        thrpt    3     535.979 ±   1080.417  ops/s
[info] ByteStringBench.oldGetInputStreamSimple  thrpt    3  486935.775 ± 222571.828  ops/s

While the speedup for complex byte strings is good and the slowdown for simple byte strings is proportionally small - it might not be worth the hit.

I presume that the MethodHandle adds an overhead.

@pjfanning
Copy link
Contributor Author

Now have after pjfanning/pekko-test@a79e14f

[info] Benchmark                                 Mode  Cnt       Score        Error  Units
[info] ByteStringBench.newGetInputStream        thrpt    3     816.413 ±    114.507  ops/s
[info] ByteStringBench.newGetInputStreamSimple  thrpt    3  492976.928 ± 145281.777  ops/s
[info] ByteStringBench.oldGetInputStream        thrpt    3     588.401 ±    117.303  ops/s
[info] ByteStringBench.oldGetInputStreamSimple  thrpt    3  490941.644 ± 119759.598  ops/s

@pjfanning pjfanning marked this pull request as ready for review April 23, 2024 13:32
@pjfanning pjfanning changed the title [WIP] use optimised ByteString.asInputStream use optimised ByteString.asInputStream Apr 23, 2024
Update Http2FrameHpackSupport.scala

Update ByteStringInputStream.scala

revert Http2FrameHpackSupport change

Update ByteStringInputStream.scala

Update ByteStringInputStream.scala
val lookup = MethodHandles.publicLookup()
val inputStreamMethodType = MethodType.methodType(classOf[InputStream])
lookup.findVirtual(classOf[ByteString], "asInputStream", inputStreamMethodType)
}.toOption
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I think we should drop the lazy and just make it a val.
  2. the toOption will introduce an allocation, use OptionVal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is only called once in the whole lifetime of the JVM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be lazy because this code may need to run unless it is needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the lazy will introduce a volatile iirc, I think this is a trade-off, just not sure if that will cause the problem in the native image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OptionVal does not support changing a Try[A] to an Option[A]. So OptionVal does not look a viable approach here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getorelse will introduce another closure creation, think we can just fallback to imperative style for performance reason

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the getOrElse has a small cost so I made a change to remove it

Update ByteStringInputStream.scala

Update ByteStringInputStream.scala
@laglangyue
Copy link
Contributor

LGTM

Copy link
Member

@He-Pin He-Pin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@pjfanning pjfanning merged commit 03f1289 into apache:main Apr 25, 2024
9 of 10 checks passed
@pjfanning pjfanning deleted the bytestring-opt branch April 25, 2024 09:38
@pjfanning pjfanning added this to the 1.1.0 milestone Apr 25, 2024
pjfanning added a commit to pjfanning/incubator-pekko-http that referenced this pull request May 10, 2024
pjfanning added a commit to pjfanning/incubator-pekko-http that referenced this pull request May 10, 2024
pjfanning added a commit that referenced this pull request May 10, 2024
@pjfanning pjfanning changed the title use optimised ByteString.asInputStream Reverted: use optimised ByteString.asInputStream May 10, 2024
pjfanning added a commit to pjfanning/incubator-pekko-http that referenced this pull request May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants